//
//  RadioHeadView.swift
//  FM
//
//  Created by x on 2017/10/31.
//  Copyright © 2017年 HLB. All rights reserved.
//

import UIKit

class RadioHeadView: UIView {
    
    
    var radio:Radio? {
        didSet {
            headBtu.setTitle(radio?.user?.nickname, for: UIControlState.normal)
            let image = SDImageCache.shared().imageFromDiskCache(forKey: radio?.cover)
            headBtu.setImage(image, for: UIControlState.normal)
            listenLable.text = "收听 " + (radio?.collectionNum)!
            followLable.text = "关注 " + (radio?.favnum)!
            contentLable.text = radio?.content
        }
    }
    

    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }
    
    func followBtuWillClick(btu:UIButton) -> Void {
        btu.isSelected = !btu.isSelected
    }
    
    func headBtuWillClick(btu:UIButton) -> Void {
        
    }
    
    /// 初始化
    func setupUI() -> Void {
        
        backgroundColor = UIColor.clear
        
        //1.添加子控件
        headBtu.translatesAutoresizingMaskIntoConstraints = false
        listenLable.textAlignment = NSTextAlignment.right
        addSubview(followBtu)
        addSubview(headBtu)
        addSubview(listenLable)
        addSubview(followLable)
        addSubview(contentLable)
        addSubview(markImage)
        
        //2.布局子控
        let dic = ["followBtu":followBtu,"headBtu":headBtu,"listenLable":listenLable,"followLable":followLable,"contentLable":contentLable,"markImage":markImage] as [String : Any]
        
        var cons:[NSLayoutConstraint] = [NSLayoutConstraint]()
        
        cons += NSLayoutConstraint.constraints(withVisualFormat: "H:[followBtu(60)]-30-|", options: NSLayoutFormatOptions.init(rawValue: 0), metrics: nil, views: dic)
        cons += NSLayoutConstraint.constraints(withVisualFormat: "V:|-30-[followBtu(30)]", options: NSLayoutFormatOptions.init(rawValue: 0), metrics: nil, views: dic)
        
        cons += NSLayoutConstraint.constraints(withVisualFormat: "H:[headBtu(50)]", options: NSLayoutFormatOptions.init(rawValue: 0), metrics: nil, views: dic)
        cons += NSLayoutConstraint.constraints(withVisualFormat: "V:|-60-[headBtu(70)]", options: NSLayoutFormatOptions.init(rawValue: 0), metrics: nil, views: dic)
        cons.append(NSLayoutConstraint.init(item: headBtu, attribute: NSLayoutAttribute.centerX, relatedBy: NSLayoutRelation.equal, toItem: self, attribute: NSLayoutAttribute.centerX, multiplier: 1.0, constant: 0))
        
        cons.append(NSLayoutConstraint.init(item: listenLable, attribute: NSLayoutAttribute.right, relatedBy: NSLayoutRelation.equal, toItem: self, attribute: NSLayoutAttribute.centerX, multiplier: 1.0, constant: 0))
        cons += NSLayoutConstraint.constraints(withVisualFormat: "V:[headBtu]-5-[listenLable]", options: NSLayoutFormatOptions.init(rawValue: 0), metrics: nil, views: dic)
        
        cons += NSLayoutConstraint.constraints(withVisualFormat: "H:[listenLable]-10-[followLable]", options: NSLayoutFormatOptions.alignAllTop, metrics: nil, views: dic)
        
        cons += NSLayoutConstraint.constraints(withVisualFormat: "V:[listenLable]-10-[contentLable]", options: NSLayoutFormatOptions.alignAllLeft, metrics: nil, views: dic)
        
        cons += NSLayoutConstraint.constraints(withVisualFormat: "H:[markImage(20)]-4-[contentLable]", options: NSLayoutFormatOptions.alignAllTop, metrics: nil, views: dic)
        cons += NSLayoutConstraint.constraints(withVisualFormat: "V:[markImage(20)]", options: NSLayoutFormatOptions.init(rawValue: 0), metrics: nil, views: dic)
        
        addConstraints(cons)
    }
    
    func headViewHeight(model:Radio) -> CGFloat {
        
        radio = model
        
        layoutIfNeeded()
        
        return contentLable.frame.maxY + 10
    }
    
    //MARK: -- 懒加载 
    
    //关注
    private lazy var followBtu:UIButton = UIButton.buttonWith(image: "unFollow", hightImage: "haveFollow", tag: 1, target: self, action: #selector(followBtuWillClick(btu:)))
    
    //头像
    private lazy var headBtu:RadioButton = RadioButton.creatRadioButton(tag: 2, target: self, action: #selector(headBtuWillClick(btu:)))
    
    private lazy var listenLable:UILabel = UILabel.lableWith(title: "", titleColor: UIColor.black, fontSize: 10)
    
    private lazy var followLable:UILabel = UILabel.lableWith(title: "", titleColor: UIColor.black, fontSize: 10)
    
    private lazy var contentLable:UILabel = {
        let tt = UILabel.lableWith(title: "", titleColor: UIColor.black, fontSize: 10)
        tt.numberOfLines = 0
        tt.lineBreakMode = NSLineBreakMode.byWordWrapping
        tt.preferredMaxLayoutWidth = 130
        
        return tt
    }()
    
    private lazy var markImage:UIImageView = UIImageView.imageWith(radios: 0, imageName: "shenfenPic")
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}
